<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 员工数据
                </el-breadcrumb-item>
                <el-button style="margin-left: 90%"
                           type="text"
                           icon="el-icon-refresh"
                           @click="getData"
                >刷新
                </el-button>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除
                </el-button>
                <el-date-picker
                    size="small"
                    @change="changeDate()"
                    v-model="vDate"
                    type="month"
                    placeholder="查询月份" style="margin-right: 5px;width: 12%">
                </el-date-picker>
                <el-button type="primary" icon="el-icon-plus" style="margin-left: 1%" @click="handleAdd">添加伴手礼销售
                </el-button>
            </div>
            <el-table
                show-summary
                :summary-method="getSummaries"
                stripe
                :data="handRecodeMonthTable"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>

                <el-table-column prop="date" label="日期" width="120" align="center">
                    <template slot-scope="scope">{{ scope.row.date| dateFormat }}</template>
                </el-table-column>


                <el-table-column prop="salesNum" label="伴手礼数量" align="center"></el-table-column>
                <el-table-column prop="vipMoney" label="会员卡充值金额" align="center">
                    <template slot-scope="scope">
                        ￥{{ scope.row.vipMoney }}
                    </template>
                </el-table-column>
                <el-table-column prop="salesMoney" label="伴手礼总价值" align="center">
                    <template slot-scope="scope">
                        ￥{{ scope.row.salesMoney }}
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-search"
                            @click="selectInfo(scope.row.date)"
                        >查看详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { getListMonth, delByTime } from '@/api/SalesHandRecode';
import format from '@/utils/format';

export default {
    name: 'basetable',
    data() {
        return {
            vDate: new Date(),
            query: {

                pageIndex: 1,
                pageSize: 90
            },
            handRecodeMonthTable: [],
            multipleSelection: [],
            drawer: false,
            pageTotal: 0,
            datesVo: {
                dates: []
            }
        };
    },
    created() {
        this.getData();
    },

    methods: {

        handleAdd() {
            this.$router.push({
                name: 'Staff'
            });
        },
        //表格合计计算
        getSummaries(param) {
            const { columns, data } = param;
            console.log('这是表格');
            console.log(param);
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 1) {
                    sums[index] = '合计 ：';
                    return;
                }
                if (index === 0) {
                    sums[index] = '';
                    return;
                }

                if (index === 5) {
                    sums[index] = format(this.query.date, 'YYYY 年 MM 月 ');
                    return;
                }
                const values = data.map(item => Number(item[column.property]));
                if (!values.every(value => isNaN(value))) {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[index] += '';
                } else {
                    sums[index] = '';
                }
            });

            return sums;
        },


        //日期选择框改变事件
        changeDate() {
            this.getData();

        },

        //编辑弹窗
        selectInfo(date) {
            //跳转每日数据
            this.$router.push({
                name: 'SalesRecodeDay',
                params: { date: date }
            });

        },


        // 获取列表
        getData() {
            console.log('======' + this.vDate);
            if (this.vDate == null || this.vDate === '') {
                this.$message.warning('请选择时间');
                return;
            }

            this.query.date = this.vDate;
            getListMonth(this.query).then(res => {
                if (res.success) {
                    this.handRecodeMonthTable = res.data.records;
                    this.pageTotal = res.data.total || 0;
                } else {
                    this.$message.warning(res.message);
                    if (res.code === 10005) {
                        this.$router.push({ path: '/login' });
                    }
                }

            });
        },
        //取消按钮
        cancel() {
            this.addVisible = false;
            //清空对象
            this.tableData = {};
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete() {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delByTime(this.datesVo).then(res => {
                    if (res.success) {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }

                });
            }).catch(() => {
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            this.datesVo.dates = [];
            for (let i = 0; i < this.multipleSelection.length; i++) {
                this.datesVo.dates.push(this.multipleSelection[i].date);

            }
            this.handleDelete();

        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>

.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.everyDay {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: #409eff;
    color: #fff;
    border-radius: 50%;
}
</style>
